package com.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.entity.Comment;
import com.repository.CommentRepository;

@RestController
@RequestMapping("/api")
public class CommentContoller {

    @Autowired
    CommentRepository commentRep;

    @GetMapping("/comment/{articleId}")
    public List<Comment> getComment(@PathVariable String articleId) {
        return commentRep.findByArticleIdOrderByCreateTimeAsc(articleId);
    }

    @PostMapping("/comment")
    public Comment insertComment(@RequestBody Comment coment) {
        coment.setCreateTime(new Date());
        return commentRep.save(coment);
    }

}
